﻿using  System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Media;
using System.Windows.Forms.Design;
using System.Collections.Generic;


namespace HaGisPack
{
    public partial class ContourLineEditForm : Form
    {
        private IWindowsFormsEditorService _Porvider; //为了自定义编辑器

        private Dictionary<double, ContourLine> _mainList = new Dictionary<double, ContourLine>(); //原来的(等值)线集合
        private List<ContourLine> _resultList = new List<ContourLine>(); //(等值)线集合

        /// <summary>
        /// 修改后的(等值)线集合
        /// </summary>
        public List<ContourLine> resultList
        {
            get { return this._resultList; }
        }


        private int _Count = 0;
        private double minVal = double.MaxValue;
        private double maxVal = double.MinValue;


        private FDContourLayer _ContourLayer;


        //public ContourLineEditForm()
        //{
        //    InitializeComponent();
        //}

        public ContourLineEditForm(IWindowsFormsEditorService DialogProvider, FDContourLayer ContourLayer)
        {
            InitializeComponent(); //窗体初始化

            _Porvider = DialogProvider;

            this._mainList.Clear();
            this.lstContour.Items.Clear();

            this._ContourLayer = ContourLayer;

            string strVal = "";

            foreach (ContourLine CL in _ContourLayer.ContourLineList)
            {
                this._mainList.Add(CL.ContourLineValue, CL);
                lstContour.Items.Add(CL.ContourLineValue.ToString()); //在控件中显示出等值线的值

             
                if (CL.ContourLineValue < this.minVal)
                    this.minVal = CL.ContourLineValue;

                if (CL.ContourLineValue > this.maxVal)
                    this.maxVal = CL.ContourLineValue;


                if (strVal == "")
                    strVal = strVal + CL.ContourLineValue.ToString();
                else
                    strVal = strVal + "," + CL.ContourLineValue.ToString();
                
            }

            this.txtSelf.Text = strVal;  //在Text控件中显示等值线的值


        }


        /// <summary>
        /// 第一种方法生成等值线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCompute1_Click(object sender, System.EventArgs e)
        {
            string LineValueStr = this.txtSelf.Text.Trim();  //用户编辑后，将编辑后的等值线值去除空格，赋值

            if (LineValueStr == "") return;  //等值线值不为空

            List<string> LineValueStrList = new List<string>(); //等值线值字符串集合形式

            int _Start = 0, _End = -1;


            string partStr;

            while (LineValueStr.IndexOf(",") != -1)
            {
                _End = LineValueStr.IndexOf(",");
                partStr = LineValueStr.Substring(_Start, _End); //单个等值线值

                if (LineValueStrList.Contains(partStr) == false) //首先要不包含这个等值线值，才添加这个等值线值
                {
                    if (partStr != "")  //等值线值不能为空
                        LineValueStrList.Add(partStr);  //添加这个等值线值 
                }

                _End++;

                string tempStr = LineValueStr.Substring(_End); //剩下的等值线值
                LineValueStr = tempStr;

            }
            //最后一个值
            _End = LineValueStr.Length;
            partStr = LineValueStr.Substring(_Start, _End);

            if (LineValueStrList.Contains(partStr) == false) 
            {
                if (partStr != "")  
                    LineValueStrList.Add(partStr); 
            }


            LineValueStrList.Sort(); //排序

            List<double> LineValueDoubleList = new List<double>(); //等值线double值集合形式
            foreach (string str in LineValueStrList)
                LineValueDoubleList.Add(double.Parse(str));

            LineValueDoubleList.Sort();

            //先清空以前的线条
            lstContour.Items.Clear();
            _mainList.Clear();

            //重新赋值与显示
            this._Count = LineValueDoubleList.Count;
            this.minVal = LineValueDoubleList[0];
            this.maxVal = LineValueDoubleList[_Count - 1];

            foreach (double val in LineValueDoubleList)
                lstContour.Items.Add(val.ToString());  //控件显示


            //以上是根据用户修改后的等值线值改变类型重新赋值存储，并检查用户修改后的等值线的值是否正确、符合要求等
            //=============================================================================//
            //以下要根据修改后的等值线值生成新的等值线，并设置其相应的其它属性:如颜色、线条宽等，最后将等值线存到_mainList中


            this.AssignMainList();            


            
        }

        /// <summary>
        /// 设置等值线相应的其它属性:如颜色、线条宽等，并存储等值线到_mainList中
        /// </summary>
        private void AssignMainList()
        {
            
            List<Color> LineColorList = MyArithmetic.BuildColor(this._ContourLayer.LineColorList); //等值线更详细的颜色集合
            int ColorNum = 0;
            if (LineColorList != null)
                ColorNum = LineColorList.Count;


            List<Color> FillColorList = MyArithmetic.BuildColor(this._ContourLayer.LineFillColorList); //填充色更详细的颜色集合
            int FillColorNum = 0;
            if (FillColorList != null)
                FillColorNum = FillColorList.Count;

            int j = 1;

            foreach (string strVal in lstContour.Items)
            {
                if (this._mainList.ContainsKey(double.Parse(strVal)))
                {
                    MessageBox.Show("值为" + strVal + "的线已经存在！");
                    return;
                }
                else
                {
                    //创建新的等值线并设置属性
                    ContourLine CL = new ContourLine();
                    CL.ContourLineValue = double.Parse(strVal);
                    if (LineColorList != null)
                        CL.ContourLineColor = LineColorList[MyArithmetic.GetColorPos(ColorNum, j, lstContour.Items.Count, this.minVal, this.maxVal)];
                    else
                        CL.ContourLineColor = Color.Empty;
                    if (FillColorList != null)
                        CL.ContourLineFillColor = FillColorList[MyArithmetic.GetColorPos(FillColorNum, j, lstContour.Items.Count, this.minVal, this.maxVal)];
                    else
                        CL.ContourLineFillColor = Color.Empty;
                    //存储到_mainList
                    this._mainList.Add(double.Parse(strVal), CL);

                }

                j++;
            }

        }

        private void txtSelf_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            switch (e.KeyChar)
            {
                case '0':
                    break;
                case '1':
                    break;
                case '2':
                    break;
                case '3':
                    break;
                case '4':
                    break;
                case '5':
                    break;
                case '6':
                    break;
                case '7':
                    break;
                case '8':
                    break;
                case '9':
                    break;
                case '.':
                    break;
                case '-':
                    break;
                case ',':
                    break;
                case '':
                    break;
                default :
                    e.Handled =true ;
                    break;

            }
                 
        }

        private void txtMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case '0':
                    break;
                case '1':
                    break;
                case '2':
                    break;
                case '3':
                    break;
                case '4':
                    break;
                case '5':
                    break;
                case '6':
                    break;
                case '7':
                    break;
                case '8':
                    break;
                case '9':
                    break;
                case '.':
                    break;
                case '-':
                    break;
                case ',':
                    break;
                case '':
                    break;
                default:
                    e.Handled = true;
                    break;

            }

        }

        private void txtMax_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case '0':
                    break;
                case '1':
                    break;
                case '2':
                    break;
                case '3':
                    break;
                case '4':
                    break;
                case '5':
                    break;
                case '6':
                    break;
                case '7':
                    break;
                case '8':
                    break;
                case '9':
                    break;
                case '.':
                    break;
                case '-':
                    break;
                case ',':
                    break;
                case '':
                    break;
                default:
                    e.Handled = true;
                    break;

            }
        }

        private void txtSpace_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case '0':
                    break;
                case '1':
                    break;
                case '2':
                    break;
                case '3':
                    break;
                case '4':
                    break;
                case '5':
                    break;
                case '6':
                    break;
                case '7':
                    break;
                case '8':
                    break;
                case '9':
                    break;
                case '.':
                    break;
                case '-':
                    break;
                case ',':
                    break;
                case '':
                    break;
                default:
                    e.Handled = true;
                    break;

            }
        }


        /// <summary>
        /// 第二种生成等值线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCompute2_Click(object sender, System.EventArgs e)
        {
            if (this.txtMin.Text == "" || this.txtMax.Text == "" || this.txtSpace.Text == "")
            {
                return;
            }


            double min = double.Parse(this.txtMin.Text);
            double max = double.Parse(this.txtMax.Text);
            double space = double.Parse(this.txtSpace.Text);

            if (min > max)
            {
                MessageBox.Show("最大值不应小于最小值");
                return;
            }

            if (space > max - min)
            {
                MessageBox.Show("间距值过大");
                return;
            }

            //清空原值
            lstContour.Items.Clear();
            this._mainList.Clear();


            //赋值
            for (int i = 0; i < System.Math.Truncate((max - min) / space + 1); i++)
            {
                lstContour.Items.Add((min + space * i).ToString());
            }

            if (double.Parse((string)lstContour.Items[lstContour.Items.Count - 1]) < max)
            {
                lstContour.Items.Add(max.ToString());
            }


            this._Count = lstContour.Items.Count - 1; //这里与付的不同，还有待验证……

            this.minVal = min;
            this.maxVal = max;

            //以上是根据用户修改后的等值线值改变类型重新赋值存储，并检查用户修改后的等值线的值是否正确、符合要求等
            //=============================================================================//
            //以下要根据修改后的等值线值生成新的等值线，并设置其相应的其它属性:如颜色、线条宽等，最后将等值线存到_mainList中

            this.AssignMainList();

        }

        private void lstContour_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (this._mainList.ContainsKey(double.Parse((string )lstContour.SelectedItem)))
            {
                

                ContourLine CL;
                bool b = this._mainList.TryGetValue(double.Parse((string)lstContour.SelectedItem), out CL);
                if (b == true)
                    this.propSub.SelectedObject = CL;
                
            }
            else
            {
                MessageBox.Show("等值线值需要重新生成");
            }
        }

        private void btnLineClr_Click(object sender, System.EventArgs e)
        {
            List<Color> LineColorList = MyArithmetic.BuildColor(this._ContourLayer.LineColorList); //等值线更详细的颜色集合
            int ColorNum = 0;
            if (LineColorList != null)
                ColorNum = LineColorList.Count;

            int j = 1;

            foreach (KeyValuePair<double, ContourLine> p in this._mainList)
            {
                p.Value.ContourLineColor = LineColorList[MyArithmetic.GetColorPos(LineColorList.Count, j, _mainList.Count, this.minVal, this.maxVal)];
                j++;
            }

        }

        private void btnPolyClr_Click(object sender, System.EventArgs e)
        {
            List<Color> FillColorList = MyArithmetic.BuildColor(this._ContourLayer.LineFillColorList); //填充色更详细的颜色集合
            int FillColorNum = 0;
            if (FillColorList != null)
                FillColorNum = FillColorList.Count;

            int j = 1;

            foreach (KeyValuePair<double, ContourLine> p in this._mainList)
            {
                p.Value.ContourLineFillColor = FillColorList[MyArithmetic.GetColorPos(FillColorList.Count, j, _mainList.Count, this.minVal, this.maxVal)];
                j++;
            }
        }

        private void btnOk_Click(object sender, System.EventArgs e)
        {
            foreach (KeyValuePair<double, ContourLine> p in this._mainList)
            {
                ContourLine val = p.Value;
                this._resultList.Add(val);
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close(); 
        }

      


        ///// <summary>
        ///// 设置等值线线颜色
        ///// </summary>
        ///// <param name="ContourLineVale">单个等值线值</param>
        ///// <param name="ContourLinePos">此等值线在等值线集合中的位置</param>
        ///// <param name="ContourLineNum">等值线集合的个数</param>
        ///// <returns></returns>
        //private Color GetContourLineColor(double ContourLineVale, int ContourLinePos, int ContourLineNum)
        //{
        //    int N = this._ContourLayer.LineColorList.Count;   //给定的渐变色个数

        //    if (N < 1) return Color.Empty;

        //    List<Color> tempColorList = new List<Color>(); //存储计算出的更加详细的渐变色

        //    if (N > 1)
        //    {
        //        Color a, b;
                
        //        for (int i=0; i < N - 1; i++)
        //        {
        //            //两个相邻的渐变色
        //            a = this._ContourLayer.LineColorList[i];
        //            b = this._ContourLayer.LineColorList[i + 1];

        //            //取其三种组成色，用于计算
        //            double aRed = a.R;
        //            double aGreen = a.G;
        //            double aBlue = a.B;
        //            double bRed = b.R;
        //            double bGreen = b.G;
        //            double bBlue = b.B;

        //            //把两个相邻的渐变色再平均分成10000份，使颜色列表更加详细……
        //            for (int j = 1; j <= 10000; j++)
        //            {
        //                double red = aRed + (bRed - aRed) / 10000 * j;
        //                double green = aGreen + (bGreen - aGreen) / 10000 * j;
        //                double blue = aBlue + (bBlue - aBlue) / 10000 * j;

        //                Color tempColor = Color.FromArgb((int)red, (int)green, (int)blue);
        //                tempColorList.Add(tempColor);
        //            }
        //        }
                
        //    }
        //    else //只有一种颜色的情况
        //    {
        //        tempColorList.Add(this._ContourLayer.LineColorList[0]);
        //    }


        //    //以上是把颜色分的更详细些，为等值线的颜色赋值做准备！！
        //    //=============================================================================//
        //    //以下就是要根据单一等值线在等值线集合中的位置为其赋相应位置的颜色！！

        //    int ColorNum = tempColorList.Count;  //详细渐变色集合 渐变色的个数……
        //    int ColorPos; //根据单一等值线在等值线集合中的位置，求得要赋予此等值线的颜色在颜色集合中的位置……


        //    if (ColorNum == 1)
        //    {
        //        ColorPos = 0;
        //    }
        //    else
        //    { 
        //        if(this.minVal !=this.maxVal )
        //        {
        //            ColorPos = ContourLinePos / ContourLineNum * ColorNum - 1;
        //        }
        //        else 
        //        {
        //            ColorPos = 0;
        //        }
        //    }


        //    if (ColorPos > ColorNum - 1) //这个判断没有意义嘛！！！！！！！！！为什么付还要写？？？？
        //        ColorPos = ColorNum - 1;

        //    if (ColorPos < 0)
        //        ColorPos = 0;

        //    return tempColorList[ColorPos];


        //}

        ///// <summary>
        ///// 设置等值线区域填充色
        ///// </summary>
        ///// <param name="ContourLineVale">单个等值线值</param>
        ///// <param name="ContourLinePos">此等值线在等值线集合中的位置</param>
        ///// <param name="ContourLineNum">等值线集合的个数</param>
        ///// <returns></returns>
        //private Color GetContourLineFillColor(double ContourLineVale, int ContourLinePos, int ContourLineNum)
        //{
        //    return Color.Empty;
        //}
     




    }
}
